package com.zhanglijie.base.threadPool;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author zhanglijie
 * @version 1.0
 * @since 1.1.0 2022/7/5 0005 0:16
 */
public class CoreThreadNumDemo {
    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2,4,1, TimeUnit.SECONDS,new LinkedBlockingQueue<>());
        System.out.println("不提交任务，线程池活跃线程数量=>"+threadPool.getPoolSize());
        boolean b = threadPool.prestartCoreThread();
        System.out.println("prestartCoreThread会开启一个核心线程，线程池活跃线程数量==>"+threadPool.getPoolSize());
        threadPool.prestartAllCoreThreads();
        System.out.println("prestartAllCoreThread会开启所有核心线程，线程池活跃线程数量==>"+threadPool.getPoolSize());
        System.out.println("当前最大线程数量==>"+threadPool.getMaximumPoolSize()+"===>getMaximumPoolSize()函数意义就是获得最大线程数");
        for (int i = 0; i < 20; i++) {
            threadPool.submit(()->{
                try {
                    Thread.sleep(500000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });

        }
        Thread.sleep(20000);
        System.out.println("一直提交任务后，线程池活跃线程数量==>"+threadPool.getPoolSize()+"====>说明getPoolSize()函数意义就是获得实际核心线程数");
        System.out.println("一直提交任务后，线程池活跃线程数量==>"+threadPool.getMaximumPoolSize()+"====>getMaximumPoolSize()函数意义就是获得最大线程数");

    }
}
